package plus.lgx.textsearcher.constant;

import plus.lgx.textsearcher.Main;
import plus.lgx.textsearcher.util.PathUtil;

import java.io.File;

import static plus.lgx.textsearcher.config.SearcherConfig.DefaultSearcherConfig.*;

/**
 * Class name: Const
 *
 * 存储全局静态常量
 *
 * Create time: 12/17/22 7:23 PM
 *
 * @author lgx
 * @version 1.0
 */
public class Const {

    /**
     * 换行符
     */
    public static final String LS = System.lineSeparator();

    /**
     * 版本号
     */
    public static final String VERSION =
            """
            Text Searcher - version: 0.5.3
            
            Written by Guangxian Liu.
            Date: 2022-12, 2023-01
            """;

    /**
     * 文件大小后缀
     */
    public static final String[] FILE_SIZE_SUFFIX = {
            "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "BB", "NB", "DB"
    };

    public static final String FILE_NAME;

    static {
        File file = new File(
                Main.class.getProtectionDomain()
                        .getCodeSource()
                        .getLocation()
                        .getPath()
        );
        while (!"/".equals(file.getAbsolutePath())
                && !file.getName().endsWith(".jar")
                && !file.getName().endsWith(".jar!")) {
            file = file.getParentFile();
        }
        String fileName = file.getName();
        if (fileName.endsWith("!")) {
            fileName = fileName.substring(0, fileName.length() - 1);
        }
        StackTraceElement[] trace = (new RuntimeException()).getStackTrace();
        String className = trace[trace.length - 1].getClassName();
        FILE_NAME = fileName.endsWith(".jar")
                ? fileName
                : className.substring(className.lastIndexOf('.') + 1);
    }

    public static final String COMMAND = FILE_NAME.endsWith(".jar")
            ? "java -jar "
            : "java ";

    public static final String CURRENT_PATH = PathUtil.standardize(new File("").getAbsolutePath(), false);

    public static final String THIS_COMMAND_FILE_PATH = CURRENT_PATH + File.separator + FILE_NAME;

//                --port=<value>       if the option SERVER is specified, use this option
//                                     to specify the port of the server, default is %d
//                                     if the option SERVER is not specified, this option
//                                     will be ignored
//                --server             startup the server
    public static final String HELP = """
        Usage: %s [OPTION]... TEXT|--search-text=<value>|--server
        Search the given text in each file of the directory,
        contains inner files of each directory.
        Or start up a server for search.
                    
        where options include:
            --blacklist=<value>  specify the blacklist of file name by regular
                                 express, the priority of blacklist is lower than
                                 the one of whitelist
            --buffer-size=<value>
                                 set the buffer size (bytes), default is %d
                                 files exceeding the buffer size will be skipped
            --console            output to the console, not to the file
                                 if this option is specified, the OUTPUT option
                                 will be ignored
            --delimiter=<value>  set the delimiter, default is "%s"
            --descent            when specified the SORT option with FILENAME or
                                 FILESIZE, use this option to invert the order
            --extend-method=[default|greedy|line|multiline[<number>]|max]
                                 setup the extend method
            -? --help            print this help message and exit
            --html               output html file (use html template)
            --ignore-case        case insensitive
            --ignore=<value>     use regular expression to specify something to
                                 ignore while searching
            --left-brackets=<values>
                                 set the left bracket, default is "%s"
            --max-extend-chars-count=<value>
                                 set the max extend chars count
                                 default is %d
            --max-file-count=<number>
                                 specify the maximum number of files to be searched,
                                 when the number of files to be searched reaches
                                 this number, the search will be stopped
            --no-pause           use this option to dislodge interval pause when
                                 the option CONSOLE is specified
            --output=<value>     the file to output, default is
                                 "%s" or "%s"
                                 supports absolute and relative paths
            --right-brackets=<values>
                                 set the right bracket, default is "%s"
            --sort=[fileName|fileSize|none|randomize]
                                 how to sort the files, default is "%s"
            --search-dir=<value> set the directory that will be search in
                                 default is the directory this command running in
            --search-text=<value>
                                 the search text, use this option if search
                                 text begins with "-"
                                 if TEXT is specified, this option will be ignored
                                 one TEXT or this option is needed
            --show-skipped=<value>
                                 set how many the skipped files which will be
                                 showed, default is %d
            --template=<value>   specify the template file path, the output
                                 will use this template
                                 default is the built-in template
            --whitelist=<value>  specify the whitelist of file name by regular
                                 express, the priority of whitelist is higher than
                                 the one of blacklist
            --version            print the version information and exit
        """.formatted(
                    COMMAND + FILE_NAME, BUFFER_SIZE,
                    new String(DELIMITER[0]).replaceAll("\n", "\\\\n"),
                    new String(DELIMITER[1]).replaceAll("\n", "\\\\n"),
                    MAX_EXTEND_CHARS_COUNT, OUTPUT_FILE_NAME,
                    OUTPUT_FILE_NAME_HTML, /*PORT,*/
                    new String(DELIMITER[2]).replaceAll("\n", "\\\\n"),
                    SORT0, SHOW_SKIPPED
            );

    public static final int MAX_TEMPLATE_FILE_LENGTH = 16 * 1024;

}
